﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Views_EmployeesFamily : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StorecbxAddManaProvince_LoadData();
        }
    }

    protected void StorecbxAddManaProvince_LoadData()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                StorecbxAddManaProvince.DataSource = db.Provinces.ToList<Province>();
                StorecbxAddManaProvince.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    protected void StoreRelationsFamily_ReadData(object sender, StoreReadDataEventArgs e)
    {
        StoreRelationFamily();
    }

    public void RowSelected(object sender, DirectEventArgs e)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = GridPanelFamily.SelectionModel.Primary as RowSelectionModel;

                int relID = Int32.Parse(rsm.SelectedRow.RecordID);
                FamilyRelationship fr = db.FamilyRelationships.Single(f => f.FaReID == relID);

                if (Boolean.Parse(fr.TypeRelation.ToString()))
                    lblTypeRelation.Text = "Bản thân";
                else
                    lblTypeRelation.Text = "Về bên vợ hoặc chồng";
                lblNameOfRelation.Text = fr.NameOfRelations;
                lblFullName.Text = fr.FullOfName;
                lblDateOfBirth.Text = DateTime.Parse(fr.YearOfBirth.ToString()).ToString("dd/MM/yyyy");
                lblJob.Text = fr.NamOfJob;
                lblPosition.Text = fr.PostionsName;
                lblOrganization.Text = fr.PlaceOfWork;
                lblAddress.Text = fr.CurrentAddress;
                lblMemberOfOrg.Text = fr.MemberOfOrganzations;
                checkViewStatus.Checked = Boolean.Parse(fr.StatusOfMemberFamily.ToString());

            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void cbxAddManaProvince_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxAddManaProvince.Value.ToString());
                Province provSelected = db.Provinces.Single(p => p.ProvID == provID);

                List<District> list = provSelected.Districts.ToList<District>();
                StoreViewDistrictByProvinces.DataSource = list;
                StoreViewDistrictByProvinces.DataBind();


                StoreAddManaCommune.DataSource = new List<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDistrict_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxAddManaDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreAddManaCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaCommune_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxAddManaCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreAddManaOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxAddManaOrganization.Value.ToString());

                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreAddManaDepartment.DataSource = org.Departments.ToList<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDepartment_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int depID = Int32.Parse(cbxAddManaDepartment.Value.ToString());

                Department dep = db.Departments.Single(o => o.DepID == depID);

                StorecbxUpdateForEmpProfiles.DataSource = dep.Employees.ToList<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    //Nhân viên
    [DirectMethod]
    public void cbxUpdateForEmpProfiles_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int employeeID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());

                if (employeeID > 0)
                {
                    ToolbarShowAdd.Disabled = false;
                }
                else
                    ToolbarShowAdd.Disabled = true;

                var result = db.Proc_FamilyRelationships_EmpID(employeeID).ToList<Proc_FamilyRelationships_EmpID_Result>();

                StoreRelationsFamily.DataSource = result;
                StoreRelationsFamily.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    public void StoreRelationFamily()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int employeeID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());

                var result = db.Proc_FamilyRelationships_EmpID(employeeID).ToList<Proc_FamilyRelationships_EmpID_Result>();

                StoreRelationsFamily.DataSource = result;
                StoreRelationsFamily.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    protected void ToolbarShowAdd_DirectClick(object sender, DirectEventArgs e)
    {
        string str = cbxUpdateForEmpProfiles.Value.ToString();
        if (str == null && str.Equals(""))
        {
            X.Msg.Alert("Thông báo", "Bạn chưa chọn nhân viên để thêm quan hệ gia đình").Show();
        }
        else
            WindownShowFormAddFamily.Hidden = false;
    }

    [DirectMethod]
    public void EmployeeFamilyRelation_Add()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int employeeID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());

                FamilyRelationship fl = new FamilyRelationship()
                {
                    EmpID = employeeID,
                    TypeRelation = RadioAddPerson.Checked,
                    NameOfRelations = TextAddNameOfRelations.Text.Trim(),
                    FullOfName = TextAddFullName.Text.Trim(),
                    YearOfBirth = DateTime.Parse(TextAddDateBirth.Text.Trim()),
                    HomeTown = TextAddHomeTown.Text.Trim(),
                    NamOfJob = TextAddJobName.Text.Trim(),
                    PostionsName = TextAddPostionName.Text.Trim(),
                    PlaceOfWork = TextAddPlaceOfWork.Text.Trim(),
                    CurrentAddress = TextAddCurrentAddress.Text.Trim(),
                    MemberOfOrganzations = TextAddMemberOfOrgan.Text.Trim(),
                    StatusOfMemberFamily = CheckAddStatus.Checked
                };
                db.FamilyRelationships.Add(fl);
                db.SaveChanges();
                StoreRelationFamily();

                WindownShowFormAddFamily.Hidden = true;

                X.Msg.Alert("Thông báo", "Thêm mới quan hệ gia đình nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void EmployeeFamilyRelation_Update(string relationID)
    {
        int relID = Int32.Parse(relationID);
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                FamilyRelationship fl = db.FamilyRelationships.Single(f => f.FaReID == relID);

                if (Boolean.Parse(fl.TypeRelation.ToString()))
                    RadioUpdatePerson.Checked = true;
                else
                    RadioUpdateNonePerson.Checked = true;

                TextUpdateNameOfRelation.Text = fl.NameOfRelations;
                TextUpdateFullName.Text = fl.FullOfName;
                TextUpdateBirth.Text = fl.YearOfBirth.ToString();
                TextUpdateHomeTown.Text = fl.HomeTown;
                TextUpdateJobName.Text = fl.NamOfJob;
                TextUpdatePositionsName.Text = fl.PostionsName;
                TextUpdatePlanceOfWork.Text = fl.PlaceOfWork;
                TextUpdateCurrentAddress.Text = fl.CurrentAddress;
                TextUpdateMemberOfOrgan.Text = fl.MemberOfOrganzations;
                CheckUpdateStatus.Checked = Boolean.Parse(fl.StatusOfMemberFamily.ToString());

                LabelID.Text = fl.FaReID.ToString();



                WindowUpdateEducations.Hidden = false;
                //X.Msg.Alert("Thông báo", "Thay đổi thông tin quan hệ gia đình nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void EmpyeeUpdate()
    {

        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int relID = Int32.Parse(LabelID.Text.ToString());

                FamilyRelationship fl = db.FamilyRelationships.Single(f => f.FaReID == relID);

                fl.TypeRelation = RadioUpdatePerson.Checked;

                fl.NameOfRelations = TextUpdateNameOfRelation.Text;
                fl.FullOfName = TextUpdateFullName.Text;
                fl.YearOfBirth = DateTime.Parse(TextUpdateBirth.Text);
                fl.HomeTown = TextUpdateHomeTown.Text;
                fl.NamOfJob = TextUpdateJobName.Text;
                fl.PostionsName = TextUpdatePositionsName.Text;
                fl.PlaceOfWork = TextUpdatePlanceOfWork.Text;
                fl.CurrentAddress = TextUpdateCurrentAddress.Text;
                fl.MemberOfOrganzations = TextUpdateMemberOfOrgan.Text;
                fl.StatusOfMemberFamily = CheckUpdateStatus.Checked;

                LabelID.Text = "";
                db.SaveChanges();
                StoreRelationFamily();

                WindowUpdateEducations.Hidden = true;
                X.Msg.Alert("Thông báo", "Thay đổi thông tin quan hệ gia đình nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }

    }

    [DirectMethod]
    public void Delete_clicked(string relID)
    {
        X.Msg.Confirm("Thông báo ", String.Format("Bạn có muốn xóa mối quan hệ này không?"),
                        new MessageBoxButtonsConfig()
                        {
                            Yes = new MessageBoxButtonConfig
                            {
                                Text = "Đóng",
                                Handler = "App.direct.FamilyRelation_Delete(" + relID + ");"
                            },
                            No = new MessageBoxButtonConfig
                            {
                                Text = "Hủy bỏ"
                            }
                        }).Show();
    }


    [DirectMethod]
    public void FamilyRelation_Delete(string relID)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int frID = Int32.Parse(relID);

                FamilyRelationship fl = db.FamilyRelationships.Single(f => f.FaReID == frID);
                db.FamilyRelationships.Remove(fl);

                db.SaveChanges();
                StoreRelationFamily();
                ResetFormView();
                WindowUpdateEducations.Hidden = true;
                X.Msg.Alert("Thông báo", "Xóa thông tin quan hệ gia đình nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    public void ResetFormView()
    {

        lblTypeRelation.Text = "";
        lblNameOfRelation.Text = "";
        lblFullName.Text = "";
        lblDateOfBirth.Text = "";
        lblJob.Text = "";
        lblPosition.Text = "";
        lblOrganization.Text = "";
        lblAddress.Text = "";
        lblMemberOfOrg.Text = "";
        checkViewStatus.Checked = false;
    }
}